# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=libimagequant
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.3.4
pkgrel=3
pkgdesc="Palette quantization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/ImageOptim/libimagequant'
msys2_references=(
  'purl: pkg:cargo/imagequant'
)
license=('spdx:GPL-3.0-or-later' OR 'custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-c")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
source=("https://github.com/ImageOptim/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('b86f4ac0eb707af88b0a000e2e2409e399c6969c9d0d988efafaff138cebaf96')

prepare() {
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cargo fetch \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml
}

build() {
  ${MINGW_PREFIX}/bin/cargo cbuild \
    --release \
    --frozen \
    --meson-paths \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml
}

package() {
  cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/cargo cinstall \
    --frozen \
    --offline \
    --meson-paths \
    --destdir="${pkgdir}" \
    --prefix=${MINGW_PREFIX} \
    --manifest-path imagequant-sys/Cargo.toml

  # Remove def file
  rm -f "${pkgdir}"${MINGW_PREFIX}/lib/*.def

  install -Dm644 CHANGELOG "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/CHANGELOG
  install -Dm644 README.md "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/README.md
  install -Dm644 COPYRIGHT "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT
}
